import { _decorator, Collider2D, Component, Animation, Contact2DType, find, instantiate, Node, Prefab, ProgressBar } from 'cc';
import { GameManager, GameState } from './GameManager';
import { EnemyManager } from './EnemyManager';
const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends Component {
    private static instance: Enemy = null;
    public static getInstnace(): Enemy {
        return this.instance;
    }
    @property
    speed: number = 300;
    @property
    hp: number = 1;

    maxHp: number = 0;

    enemyCollider: Collider2D = null;

    @property(Prefab)
    bulletPrefabs_0: Prefab = null;

    @property(Prefab)
    bulletPrefabs_1: Prefab = null;

    @property
    canShoot: boolean = true;

    private lastBulletTime: number = 0; // 上一次生成子弹的时间
    private spLastBulletTime: number = 0;  //特别子弹

    @property(ProgressBar)
    progressBar: ProgressBar = null;  // 引用 ProgressBar 组件

    @property(Animation)
    anim: Animation = null;

    haveDead: boolean = false;

    @property
    deadGold:number=10      //enemy死亡获取的金币
    

    start() {
        Enemy.instance=this;


        this.enemyCollider = this.node.getComponent(Collider2D);
        if (this.enemyCollider) {
            this.enemyCollider.on(Contact2DType.BEGIN_CONTACT, this.attacked, this);
        }

        this.maxHp = this.hp;

    }

    update(deltaTime: number) {
        
        if (this.hp > 0&&GameManager.getInstance().gameState == GameState.START) {
            const newPos = this.node.position.clone();
            this.node.setPosition(newPos.x, newPos.y - this.speed * deltaTime, newPos.z);

        }

        if (this.node.position.y < -800) {
            if(this.node){
                this.node.destroy();
            }
        }

        const currentTime = Date.now();
        if (currentTime - this.lastBulletTime >= 2000) {        //每隔毫秒调用
            this.lastBulletTime = currentTime;

            if (this.canShoot) {
                this.shoot();
            }
        }

    }
    
    protected onDestroy(): void {
        if (this.enemyCollider) {
            this.enemyCollider.off(Contact2DType.BEGIN_CONTACT, this.attacked, this);
        }

        EnemyManager.getInstance().removeEnemy(this.node);

        // GameManager.getInstance().addGold(this.deadGold)
        // console.log(GameManager.getInstance().getGoldCount());
    }

    attacked(selfCollider: Collider2D, otherCollider: Collider2D) {
        if (otherCollider.group == 32) {
            const currentTime = Date.now();
            if (currentTime - this.spLastBulletTime >= 40) {        //每隔毫秒调用
                this.spLastBulletTime = currentTime;
                this.hp -= 1;
            }
        } else if (otherCollider.group == 2) {
            this.hp = 0;
        } else {
            this.hp -= 1
        }

        if(this.hp<=0){
            if(this.node){
                this.anim.play("EnemyDead");

            }

            this.dead();
        }
        this.updateHealthBar();  // 更新血条
    }


    dead() {
        if (this.haveDead) return;
        this.haveDead = true;
        GameManager.getInstance().addGold(this.deadGold)

        if (this.enemyCollider) {
            this.enemyCollider.enabled = false;
        }

        this.scheduleOnce(function () {
            if(this.node){
                this.node.destroy();
            }
        }, 0.6);

    }


    shoot() {
        if (this.haveDead) return
        const newBullet = instantiate(this.bulletPrefabs_0);

        const newNode = find("Canvas/BG/Bullets");
        newNode.addChild(newBullet);

        const newPos = this.node.getWorldPosition();
        newBullet.setWorldPosition(newPos);


    }
    // 更新血条的进度
    updateHealthBar() {
        if (this.progressBar) {
            const healthRatio = this.hp / this.maxHp;  // 你可以根据最大血量来调整比例，这里假设最大血量为 1
            this.progressBar.progress = healthRatio;
        }
    }
    
    killNow() {
        if (this.hp <= 0) return;
        this.hp = 0;
        this.anim.play("EnemyDead");
        this.dead();
    }
}


